const gulp = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const minifycss = require('gulp-minify-css');
const htmlmin = require('gulp-htmlmin');
const rev = require('gulp-rev');
const revCollector = require('gulp-rev-collector');
const smushit = require('gulp-smushit');
const del = require('del');
const gulpSequence = require('gulp-sequence');
const ftp = require('gulp-sftp');
const fs = require('fs');
const gulpIf = require('gulp-if');

const ftpConfig = JSON.parse(fs.readFileSync('config/ftp.config.json'))


const argv = process.argv.splice(2);
let param = new Object();
if (argv.length % 2 != 0) {
    console.log('参数不符合要求');
    return;
}
for (let i = 0; i < argv.length; i++) {
    if (i % 2 == 0 && i < argv.length) {
        param[argv[i].replace('-', '')] = argv[i + 1]
    }

}

var options = {
    removeComments: true, //清除HTML注释
    collapseWhitespace: true, //压缩HTML
    minfyJS: true,//压缩JS
    minfyCss: true,//压缩CSS
};

gulp.task('copyLib', function () {
    return gulp.src('lib/*')
        .pipe(gulp.dest('dist/lib'))
});

gulp.task('copyStatic', function () {
    return gulp.src('static/*')
        .pipe(gulp.dest('dist/static'))
});

gulp.task('copyStatic', function () {
    return gulp.src('static/*')
        .pipe(gulp.dest('dist/static'))
});

gulp.task('miniImages', function () {
    return gulp.src('status/images/*')
        .pipe(smushit({
            verbose: true
        }))
        .pipe(gulp.dest('dist/status/images'))
});

gulp.task('miniStyle', function () {
    return gulp.src('style/*.css')      //设置css
        .pipe(minifycss())                    //压缩文件
        .pipe(rev())   //css添加后缀MD5
        .pipe(gulp.dest('dist/style'))           //设置输出路径
        .pipe(rev.manifest())  //输出新css文件名与原生文件名映射内容
        .pipe(gulp.dest('mapper/style')); //输出内容到指定文件夹

});

gulp.task('miniImport', function () {
    return gulp.src('import/*.js')// ES6 源码存放的路径
        .pipe(babel({
            presets: ['es2015']
        }))   //使用es5的标准转化es6
        .pipe(uglify())
        .pipe(rev())   //js添加后缀MD5
        .pipe(gulp.dest('dist/import')) //转换成 ES5 存放的路径
        .pipe(rev.manifest())  //输出新js文件名与原生文件名映射内容
        .pipe(gulp.dest('mapper/js')); //输出内容到指定文件夹
});
gulp.task('miniUtils', function () {
    return gulp.src('utils/*.js')// ES6 源码存放的路径
        .pipe(babel({
            presets: ['es2015']
        }))   //使用es5的标准转化es6
        .pipe(uglify())
        .pipe(rev())   //js添加后缀MD5
        .pipe(gulp.dest('dist/utils')) //转换成 ES5 存放的路径
        .pipe(rev.manifest())  //输出新js文件名与原生文件名映射内容
        .pipe(gulp.dest('mapper/utils')); //输出内容到指定文件夹
});


gulp.task('miniHtml', function () {
    return gulp.src(['mapper/**/*.json', 'pages/*.html'])// 页面存放位置
        .pipe(revCollector())  //根据映射器替换文件
        .pipe(htmlmin(options))
        .pipe(gulp.dest('dist/pages')); //压缩后的位置
});

gulp.task('clean', function () {
    // del(['dist/pages/js'])
})

gulp.task('uploadFtp', function () {
    if (param.mode == 'production') {
        console.log('通过ftp部署到服务器...');
        var ftpAuth = ftpConfig.ftp;
        try {
            return gulp.src('.' + ftpConfig.dist + '**')
                .pipe(ftp(ftpAuth))
        } catch (error) {
            console.log('上传错误')
        }


    } else {
        console.log('开发环境');
        return this;
    }

})


gulp.task('default', gulpSequence('copyLib', 'miniImages', 'copyStatic', 'miniStyle', 'miniImport', 'miniUtils', 'miniHtml', 'clean', 'uploadFtp'));